<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\model;

use think\Model;
use think\Db;

/**
 * 商品
 * @package app\admin\model
 */
class Product extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__GOODS_LIST__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
    *查询商品
    */
    public static function goodsSelect($map = [], $order = '')
    {
        $data_list = self::view('goods_list', true)
            ->view('goods_class', 'artno classname', 'goods_class.classid=goods_list.classid', 'left')
            ->view('goods_color',  'Name colorname,customcode Colorcustomcode',  'goods_color.colorid=goods_list.colorid','left')
            ->view('goods_size',   'Name sizename',   'goods_size.sizeid=goods_list.sizeid','left')
            ->where($map)
            ->order($order)
            ->paginate();
        foreach ($data_list as $key => $value) {
            $data_list[$key]['colorname'] = $value['Colorcustomcode'].'/'.$value['colorname'];
        }
        return $data_list;
    }

    public static function show($id = 0){
        $data_list = self::view('goods_list', true)
            ->view('goods_class',  'artno classname', 'goods_class.classid=goods_list.classid', 'left')
            ->view('goods_colorgroup', 'groupname colorgroupname', 'goods_colorgroup.colorgroupid=goods_list.colorgroupid','left')
            ->view('goods_color',  'Name colorname',  'goods_color.colorid=goods_list.colorid','left')
            ->view('goods_sizegroup', 'groupname sizegroupname', 'goods_sizegroup.sizegroupid=goods_list.sizegroupid','left')
            ->view('goods_size',   'Name sizename',   'goods_size.sizeid=goods_list.sizeid','left')
            ->view('goods_brand',  'brandname', 'goods_brand.brandid=goods_list.brandid', 'left')
            ->view('shop', 'name shopname', 'shop.id=goods_list.ownerid', 'left')
            //->view('goods_coderule', 'codename', 'goods_coderule.codeid=goods_list.codeid', 'left')
            ->where('goods_list.goodsid='.$id)
            ->find();
            $data_list['codename'] = Db::name('goods_coderule')->where('codeid', $data_list['codeid'])->value('codename');
            $data_list['activitesid'] = Db::name('goods_activites')->where('activitesid', $data_list['activitesid'])->value('activitesname');
            if($data_list['checkstatus'] == 1){
                $data_list['checkstatus'] = '已审核';
            }else{
                $data_list['checkstatus'] = '未审核';
            }
            if($data_list['bedit'] == 1){
                $data_list['bedit'] = '已审核';
            }else{
                $data_list['bedit'] = '未审核';
            }

            if($data_list['status'] == 1){
                $data_list['status'] = '上架';
            }else{
                $data_list['status'] = '下架';
            }
            if($data_list['minprice'] == 0){
                $data_list['minprice'] = '无';
            }
        return $data_list;
    }

    /**
    *导出查询
    */

    public static function exportSelect($map = [], $order = '', $p = '1')
    {
        $data_list = self::view('goods_list', true)
            ->view('goods_class', 'artno classname', 'goods_class.classid=goods_list.classid', 'left')
            ->view('goods_colorgroup', 'groupname colorgroupname', 'goods_colorgroup.colorgroupid=goods_list.colorgroupid','left')
            ->view('goods_color',  'Name colorname',  'goods_color.colorid=goods_list.colorid','left')
            ->view('goods_sizegroup', 'groupname sizegroupname', 'goods_sizegroup.sizegroupid=goods_list.sizegroupid','left')
            ->view('goods_size',   'Name sizename',   'goods_size.sizeid=goods_list.sizeid','left')
            ->view('goods_brand', 'brandname', 'goods_brand.brandid=goods_list.brandid', 'left')
            ->where($map)
            ->order($order)
            ->page($p, 1000)
            ->select();

        foreach ($data_list as $key => $value) {
            if($value['checkstatus'] == 1){
                $data_list[$key]['checkstatus'] = '已审核';
            }else{
                $data_list[$key]['checkstatus'] = '未审核';
            }
            if($value['bedit'] == 1){
                $data_list[$key]['bedit'] = '已审核';
            }else{
                $data_list[$key]['bedit'] = '未审核';
            }

            if($value['status'] == 1){
                $data_list[$key]['status'] = '上架';
            }else{
                $data_list[$key]['status'] = '下架';
            }

            if($value['minprice'] == 0){
                $data_list[$key]['minprice'] = '无';
            }
        }
        return $data_list;
    }


    
  
}